 
package com.farsunset.ichat.cim.session;

import java.util.Collection;

import com.appleframework.cache.core.CacheManager;
import com.appleframework.cache.redis.RedisCacheManager3;
import com.farsunset.cim.nio.session.ClusterSession;
import com.farsunset.ichat.common.util.StringUtil;

/** 
 * 集群 session管理实现
 */
public class ClusterSessionManager {    
    
	private CacheManager cacheManager;

    public void addSession(String account, ClusterSession session) {         
    	 if(session!=null && StringUtil.isNotEmpty(session.getAccount())) {         	
         	cacheManager.set(account, session);
         }        
    }
     
    public ClusterSession getSession(String account) {    	
    	ClusterSession session = cacheManager.get(account,ClusterSession.class);
         return session;
    }
     
    public Collection<Object> getSessions() { 
    	return	((RedisCacheManager3)cacheManager).getCacheMap().values();
    }
 
    public void  removeSession(ClusterSession session) {        
    	cacheManager.remove((String)session.getAccount());
    }
     
    public void  removeSession(String account) {
    	cacheManager.remove(account);  
    	
    }    
    
    public boolean containsClusterSession(ClusterSession ios) {    	
    	return cacheManager.get(ios.getAccount(), ClusterSession.class) != null;   	
    }

    
    public String getAccount(ClusterSession ios) {
       return ios.getAccount();
    }

	public CacheManager getCacheManager() {
		return cacheManager;
	}

	public void setCacheManager(CacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}        
	
 
}
